package com.wejoy.video.middle.station.admin.controller.system;

import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysLog;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.service.system.ISysLogService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 日志表
 *
 * @author admin
 * @date 2022年09月16日 12:41:35
 */
@RestController
@RequestMapping("/system/sysLog")
public class SysLogController extends BaseController {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * 保存或更新
     *
     * @param sysLogReq sysLogReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "sys:log:add")
    public RespJSON save(@RequestBody SysLog sysLogReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        sysLogService.save(sysLogReq, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysLogReq sysLogReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys:log:list")
    public RespJSON getPageList(@RequestBody SysLog sysLogReq) {
        int count = sysLogService.getCount(sysLogReq);
        List<SysLog> list = new ArrayList<>();
        if (count > 0) {
            list = sysLogService.getPageList(sysLogReq);
        }
        PageResult result = new PageResult(sysLogReq.getPageNo(), sysLogReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(sysLogService.getOneById(id));
    }

    /**
     * 审核
     *
     * @param id id
     * @return RespJSON
     */
    @PostMapping("/audit")
    @ResponseBody
    public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysLogService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 取消审核
     *
     * @param id id
     * @return RespJSON
     */
    @PostMapping("/unaudit")
    @ResponseBody
    public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysLogService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

}
